﻿package com.oyjd.beauty.business.goods.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.beauty.bean.vo.GoodsTypeVO;
import com.oyjd.beauty.bean.vo.GoodsVO;
import com.oyjd.beauty.bean.vo.UserVO;
import com.oyjd.beauty.business.goods.service.GoodsService;
import com.oyjd.beauty.business.goods.vo.GoodsVOEx;
import com.oyjd.beauty.fw.action.BaseAction;
import com.oyjd.beauty.fw.action.Page;
import com.oyjd.beauty.fw.util.CodeUtil;
import com.oyjd.beauty.fw.util.OutUtil;
import com.oyjd.beauty.pub.Const;

/**
 * 
 * 商品信息Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/goods")
@Results({ @Result(name = "viewList", location = Const.JSP_goods_list), @Result(name = "viewAdd", location = Const.JSP_goods_add), @Result(name = "viewEdit", location = Const.JSP_goods_edit), @Result(name = "viewTypeList", location = Const.JSP_goods_order), @Result(name = "viewLeftList", location = Const.JSP_goods_orderDx) })
public class GoodsAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(GoodsAction.class);
	private GoodsService service;

	@Autowired
	public void setService(GoodsService service) {
		this.service = service;
	}

	/**
	 * 进入商品信息列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	/**
	 * 进入商品信息新增页面
	 * 
	 * @return
	 */
	public String viewAdd() {
		return "viewAdd";
	}

	/**
	 * 进入商品信息修改页面
	 * 
	 * @return
	 */
	public String viewEdit() {
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			GoodsVO vo = (GoodsVO) service.getGoodsVO(id);
			List<String> bodyList = service.getBody(id);
			List<String> kidList = service.getKid(id);
			req.setAttribute("data", vo);
			req.setAttribute("bodyList", bodyList);
			req.setAttribute("kidList", kidList);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewEdit";
	}

	/**
	 * 获取商品信息列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<GoodsVOEx> page = service.getList(new Page<GoodsVOEx>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	// 上传文件域对象
	private File pic;
	// 上传文件名
	private String picFileName;

	public void setPic(File pic) {
		this.pic = pic;
	}

	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}

	/**
	 * 新增商品信息
	 * 
	 * @return
	 */
	public String add() {
		try {
			HttpServletRequest req = getRequest();
			String goodsTypeId = ServletRequestUtils.getStringParameter(req, "goodsTypeId");
			String link = ServletRequestUtils.getStringParameter(req, "link");
			String title = ServletRequestUtils.getStringParameter(req, "title");
			String suggest = ServletRequestUtils.getStringParameter(req, "suggest");
			String[] kids = ServletRequestUtils.getStringParameters(req, "kid");
			String[] bodys = ServletRequestUtils.getStringParameters(req, "body");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.add(goodsTypeId, link, title, suggest, picFileName, pic, kids, bodys, userVO.getAccount());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewList";
	}

	/**
	 * 编辑商品信息
	 * 
	 * @return
	 */
	public String edit() {
		try {
			HttpServletRequest req = getRequest();
			String goodsId = ServletRequestUtils.getStringParameter(req, "goodsId");
			String goodsTypeId = ServletRequestUtils.getStringParameter(req, "goodsTypeId");
			String link = ServletRequestUtils.getStringParameter(req, "link");
			String title = ServletRequestUtils.getStringParameter(req, "title");
			String suggest = ServletRequestUtils.getStringParameter(req, "suggest");
			String[] kids = ServletRequestUtils.getStringParameters(req, "kid");
			String[] bodys = ServletRequestUtils.getStringParameters(req, "body");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.edit(goodsId, goodsTypeId, link, title, suggest, picFileName, pic, kids, bodys, userVO.getAccount());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewList";
	}

	/**
	 * 删除商品信息
	 * 
	 * @return
	 */
	public String remove() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.remove(cbs, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取图片文件流
	 * 
	 * @return
	 */
	public String getSou() {
		try {
			HttpServletRequest req = getRequest();
			String picName = ServletRequestUtils.getStringParameter(req, "picName");
			byte[] b = service.getSou(picName);
			if (null != b && b.length > 0) {
				OutUtil.outFile(b, picName, getResponse());
			} else {
				log.error("无效资源[picName=" + picName + "]");
			}
		} catch (ServletRequestBindingException err) {
			log.error(err.getMessage(), err);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return null;
	}

	/**
	 * 进入商品排序信息列表页面
	 * 
	 * @return
	 */
	public String viewTypeList() {
		try {
			HttpServletRequest req = getRequest();
			String type = ServletRequestUtils.getStringParameter(req, "type");
			req.setAttribute("type", type);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewTypeList";
	}

	/**
	 * 进入商品排序差异信息列表页面
	 * 
	 * @return
	 */
	public String viewLeftList() {		
		try {
			HttpServletRequest req = getRequest();
			String type = ServletRequestUtils.getStringParameter(req, "type");
			req.setAttribute("type", type);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewLeftList";
	}

	/**
	 * 交换资料的排序位置
	 * 
	 * @return
	 */
	public String exchangePos() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String type = ServletRequestUtils.getStringParameter(req, "type");
			String id1 = ServletRequestUtils.getStringParameter(req, "id1");
			String id2 = ServletRequestUtils.getStringParameter(req, "id2");
			service.exchangePos(id1, id2, type);
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 将资料的排至首位
	 * 
	 * @return
	 */
	public String moveFirst() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String type = ServletRequestUtils.getStringParameter(req, "type");
			String id = ServletRequestUtils.getStringParameter(req, "id");
			service.moveFirst(id, type);
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增类型排序元素
	 * 
	 * @return
	 */
	public String addOrder() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String type = ServletRequestUtils.getStringParameter(req, "type");
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			service.addOrder(cbs, type);
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 删除类型排序元素
	 * 
	 * @return
	 */
	public String removeOrder() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String type = ServletRequestUtils.getStringParameter(req, "type");
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			service.removeOrder(cbs, type);
			//
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取大分类
	 * 
	 * @return
	 */
	public String getTopType() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			List<GoodsTypeVO> list = service.getTopType();
			map.put("info", list);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 查询资源特定小分类列表
	 * 
	 * @return
	 */
	public String getTypeList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String type = ServletRequestUtils.getStringParameter(req, "type");
			Page<GoodsVOEx> page = service.getTypeList(new Page<GoodsVOEx>(req), type);
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (ServletRequestBindingException err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.PARA_ERR);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取差异的列表
	 * 
	 * @return
	 */
	public String getLeftList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String type = ServletRequestUtils.getStringParameter(req, "type");
			Page<GoodsVOEx> page = service.getLeftList(new Page<GoodsVOEx>(req), type);
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
}
